<?php

namespace App\Http\Controllers\Family;

use App\Http\Controllers\Controller;
use App\Models\Child;
use App\Models\Grade;
use App\Models\History;
use Illuminate\Http\Request;

class AnalysisController extends BaseController
{
    public function issuccess($child_id = 0)
    {
        return familyView('analysis.issuccess', ['child_id' => $child_id]);
    }

    public function getsuccessdata($child_id = 0)
    {

        $child = Child::find($child_id);

        if (empty($child)) {
            return response()->json(['status' => 404, 'data' => []]);
        }

        if (!$this->allow("family", $child)) {
            return response()->json(['status' => 400, 'data' => [], 'msg' => __('Sorry, you are forbidden from accessing this page.')]);
        }

        $list = History::where("child_id", $child_id)->selectRaw('result, grade_id, count(id) as num')->with('grade')
            ->groupBy('result', 'grade_id')
            ->orderBy('grade_id')
            ->get();


        $result = $tmp = [];
        foreach ($list as $v) {
            $tmp[$v->result][$v->grade_id] = $v->num;
        }
        for ($i = 0; $i < 2; $i++) {
            for ($j = 1; $j < 4; $j++) {
                isset($tmp[$i][$j]) || $tmp[$i][$j] = 0;
                $result[$i][] = $tmp[$i][$j];
            }
        }

        return response()->json(['status' => 200, 'data' => $result]);
    }

    public function trainingcount($child_id)
    {
        $grades = Grade::all();
        return familyView('analysis.trainingcount', ['child_id' => $child_id, 'grades'=>$grades]);
    }

    public function basecount($child_id = 0)
    {
        return frontendView('analysis.trainingcount', ['type' => 1, 'child_id' => $child_id]);
    }

    public function middlecount($child_id = 0)
    {
        return frontendView('analysis.trainingcount', ['type' => 2, 'child_id' => $child_id]);
    }

    public function highcount($child_id = 0)
    {
        return frontendView('analysis.trainingcount', ['type' => 3, 'child_id' => $child_id]);
    }

    public function gettrainingdata($child_id = 0, Request $request)
    {
        $user = $this->getUser();
        $type = $request->input("type");
        if (!in_array($type, [1, 2, 3])) {
            $type = 1;
        }
        //todu:暂时考虑所有记录，如果需要可以调整为成功记录，where('result', 1)

        $list = History::where('child_id', $child_id)
            ->where('grade_id', $type)
            ->selectRaw('grade_id, game_id, count(id) as num')
            ->with('game')
            ->groupBy('grade_id', 'game_id')
            ->orderBy('num', 'desc')
            ->get();


        $result = ['x' => [], 'y' => [], 'z' => []];

        foreach ($list as $v) {
            $result['x'][] = $v->game->name;
            $result['y'][] = $v->num;
            //$result['z'][] = $v->game_id;
        }

        return response()->json(['status' => 200, 'data' => $result]);
    }

    public function view($id)
    {
        $training = History::find($id);
        if (empty($training)) {
            return back()->with('error', __('The page you requested was not found'))->withInput();
        }

        $user = $this->getUser();
        if (!$user->can("allow", $training->child)) {
            return back()->with('error', __("Sorry, you are not authorized to access this page."));
        }
        //dd($training);
        return frontendView('training.view', compact('training'));
    }
}
